package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.domain.TbClue;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessTrackRecordMapper trackRecordMapper;

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Autowired
    private ISysDictDataService sysDictDataService;


    /**
     * 根据商机id查询商机跟进记录
     *
     * @param id
     * @return
     */
    @Override
    public List<TbBusinessTrackRecord> list(Long id) {
        //根据商机id查询商机跟进记录
        List<TbBusinessTrackRecord> list = trackRecordMapper.getRecordByBusinessId(id);
        for (TbBusinessTrackRecord trackRecord : list) {
            //遍历记录去字典里面查询意向名称
            String keyItem = trackRecord.getKeyItems();
            if (keyItem != null){
                String[] keyItems = keyItem.split(",");
                for (String s : keyItems) {
                    //根据类型和值获取名称
                    String label = sysDictDataService.selectDictLabel("communication_point",s);
                    trackRecord.getKeys().add(label);
                }
            }
        }
        return list;
    }

    /**
     * 新增商机跟进记录
     * @param businessTrackVo
     * @return
     */
    @Override
    @Transactional
    public int add(BusinessTrackVo businessTrackVo) {
        //先更新商机表中的内容
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo, tbBusiness);
        //设置商机状态为跟进中
        tbBusiness.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        tbBusiness.setId(businessTrackVo.getBusinessId());
        tbBusinessMapper.updateTbBusiness(tbBusiness);

        //再添加商机跟进记录
        TbBusinessTrackRecord trackRecord = new TbBusinessTrackRecord();
        trackRecord.setCreateTime(DateUtils.getNowDate());
        trackRecord.setCreateBy(SecurityUtils.getUsername());
        BeanUtils.copyProperties(businessTrackVo, trackRecord);
        return trackRecordMapper.insert(trackRecord);
    }

    /**
     * 退回公海
     * @return
     */
    @Override
    @Transactional
    public int back(Long id,String reason) {
        //根据id查询商机信息
        TbBusiness business = tbBusinessMapper.selectTbBusinessById(id);

        //添加商机跟进记录  添加退回原因和状态
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(business, tbBusinessTrackRecord,"id");
        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        tbBusinessTrackRecord.setCreateTime(DateUtils.getNowDate());
        tbBusinessTrackRecord.setBusinessId(id);
        //从字典里查询退回原因
        String label = sysDictDataService.selectDictLabel("reasons_for_business_reporting", reason);
        tbBusinessTrackRecord.setRecord(label);

        //修改状态为退回公海
        TbBusiness tbBusiness = new TbBusiness();
        tbBusiness.setId(business.getId());
        tbBusiness.setStatus("4");
        tbBusinessMapper.updateTbBusiness(tbBusiness);

        return  trackRecordMapper.insert(tbBusinessTrackRecord);
    }
}
